<template>
  <div class="login">
    <LoginHeader>
      <el-form :rules="rules" :model="ruleForm" ref="ruleForm" label-position="left" label-width="0px" slot="container">
        <div class="title">
          <h3>账号密码登录</h3>
        </div>
        <!-- username -->
        <el-form-item prop="userAccount">
          <el-input type="text" v-model="ruleForm.userAccount" auto-complete="off" placeholder="账号">
            <i slot="prefix" class="fa fa-user-o"></i>
          </el-input>
        </el-form-item>

        <!-- password -->
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleForm.password" auto-complete="off" placeholder="密码">
            <i slot="prefix" class="fa fa-lock"></i>
          </el-input>
        </el-form-item>

        <!-- 登录button -->
        <el-form-item>
          <el-button :loading="isLogin" @click.native.prevent="handleSubmit"  type="primary" style="width:100%;">登录</el-button>
        </el-form-item>
      </el-form>
    </LoginHeader>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import LoginHeader from "./LoginHeader.vue";

@Component({
  components: { LoginHeader }
})
export default class Login extends Vue {
  isLogin: boolean = false;
  ruleForm: {
    userAccount: String;
    password: String;
    autoLogin: boolean;
  } = {
    userAccount: "",
    password: "",
    autoLogin: true // 是否自动登录
  };

  rules = {
    userAccount: [{ required: true, message: "请输入账号", trigger: "blur" }],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }]
  };

  handleSubmit(): void {
    (this.$refs["ruleForm"] as any).validate((valid: boolean) => {
      if (valid) {
        this.isLogin = true;
        (this as any).$axios.post("/adminUser/login", this.ruleForm).then((res: any) => {
      
          if(res.data.code === 200){
            this.isLogin = false;
            (this as any).$message({
              message: '登录成功',
              type: "success"
            })
            // 存储token
            localStorage.setItem("tsToken", res.data.data.token);
            // 登录成功 跳转 /
            this.$router.push("/");
          }
          else{
            this.isLogin = false;
            (this as any).$message.error(res.data.data)
          }
        })
        .catch(() => {
          this.isLogin = false;
        });
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.login{
  width: 100%;
  min-width: 1200px !important;
  height: 100%;
}
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

i {
  font-size: 14px;
  margin-left: 8px;
}
.forget {
  float: right;
}
</style>

